var wxHandle = (function ($, window) {

    //初始化微信配置
    function _init() {

        if (window['G_WX_CONFIG']) {
            _setWxConfig(window['G_WX_CONFIG']);
        } else {
            //获取wxConfig
            $.ajax({
                async: false,
                type: "POST",
                url: ("/api/weixin/sdk.php"),
                data: {
                    url: location.href.split("#")[0]
                },
                success: function (result) {
                    if (result.appId) {
                        _setWxConfig(result);
                    } else {
                        console.log("微信初始化加载失败");
                    }
                }
            });
        }

    }

    function _setWxConfig(result) {
        wx.config({
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            appId: result.appId, // 必填，公众号的唯一标识
            timestamp: result.timestamp, // 必填，生成签名的时间戳
            nonceStr: result.nonceStr, // 必填，生成签名的随机串
            signature: result.signature, // 必填，签名，见附录1
            url: result.url,
            jsApiList: [ // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareWeibo',
                'hideMenuItems',
                'showMenuItems',
                'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem',
                'translateVoice',
                'startRecord',
                'stopRecord',
                'onRecordEnd',
                'playVoice',
                'pauseVoice',
                'stopVoice',
                'uploadVoice',
                'downloadVoice',
                'chooseImage',
                'previewImage',
                'uploadImage',
                'downloadImage',
                'getNetworkType',
                'openLocation',
                'getLocation',
                'hideOptionMenu',
                'showOptionMenu',
                'closeWindow',
                'scanQRCode',
                'chooseWXPay',
                'openProductSpecificView',
                'addCard',
                'chooseCard',
                'openCard'
            ]
        });

    }

    function _setShare(params) {
        params = $.extend({
            title: '', //分享标题
            desc: '', //分享内容
            link: '', //分享连接
            imgUrl: '', //分享图片地址
            success: function () {
                //alert('分享成功！');
            }
        }, params);

        //分享给朋友
        wx.onMenuShareAppMessage(params);
        var timelineParams = $.extend({}, params);
        timelineParams.title = timelineParams.desc;
        //分享到朋友圈
        wx.onMenuShareTimeline(timelineParams);
        //分享到QQ
        wx.onMenuShareQQ(params);
        //分享到微博
        wx.onMenuShareWeibo(params);
    }

    return {
        init: _init,
        setShare: _setShare

    }
})(Zepto, window);

//默认微信分享
wxHandle.init();
wx.ready(function () {
    wxHandle.setShare({
        title: 'vivo 急速挑战', //分享标题
        desc: '你够快吗？vivo X6急速任务等你来挑战！', //分享内容
        link: 'http://vivo.geemedia.com.cn/?v=' + new Date().getTime(), //分享连接
        imgUrl: 'http://vivo.geemedia.com.cn/styles/images/weixin.jpg' //分享图片地址
    });
});
